package com.taotao.search.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.common.pojo.SearchResult;
import com.taotao.search.service.SearchItemService;

/**
 * 索引查询controller
 * @author leaf
 */
@Controller
public class IndexSearchController {
	
	@Autowired
	private SearchItemService searchItemService;
	
	@Value("${ITEM_ROWS}")
	private Integer ITEM_ROWS;
	
	@RequestMapping("/search")
	public String IndexSearch(@RequestParam("q") String queryStr,@RequestParam(defaultValue="1") Integer page,Model model) throws Exception{
		queryStr = new String (queryStr.getBytes("iso8859-1"),"UTF-8");
		SearchResult result = searchItemService.getSearchResult(queryStr, page, ITEM_ROWS);
		model.addAttribute("query", queryStr);
		model.addAttribute("totalPages", result.getPageCount());
		model.addAttribute("itemList", result.getItemList());
		model.addAttribute("page", page);
		//int i = 1 / 0;
		return "search";
	}
}
